package com.cip.admin.action;

import com.cip.admin.model.Chat;
import com.cip.admin.model.User;
import com.cip.admin.service.CIPLoggerService;
import com.cip.admin.service.ChatService;
import com.cip.common.util.CommonUtil;
import com.cip.common.util.SecurityUtil;
import com.cip.webapp.action.BaseAction;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

public class ChatAction extends BaseAction{
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private ChatService chatService;
	private CIPLoggerService cipLoggerService;
	
		
	/**
	 *  Get all users from account table 
	 *  to display in chat online users
	 */
	public void startChatSession() throws Exception{
		JSONObject jsonobject = new JSONObject();
		User user = SecurityUtil.getThreadLocalUser().get();
		List<String> userList= cipLoggerService.getAllLoggedFriendsList(user.getId());
		userList.remove(user.getUsername());
		try{
			
			jsonobject.put("username", user.getUsername());
			jsonobject.put("userName", userList);
			
		}catch(Exception e){
			e.printStackTrace();
		}finally{
			CommonUtil.generateResponseThroughJSON(getResponse(), jsonobject);
		}
		
	}
	
	/**
	 * to save the chat content in table
	 * 
	 */
	public void sendChat(){
		try{
			String to = getRequest().getParameter("to");
			String message = getRequest().getParameter("message");
			Chat chat =  new Chat();
			chat.setChatFrom(SecurityUtil.getLoggedInUser().getUsername());
			chat.setChatTo(to);
			chat.setMessage(message);
			chat.setIsReceived(0);
			chatService.saveOrUpdateChat(chat);
			System.out.println("chat ms save success fullty====");
		}catch(Exception e){
			e.printStackTrace();
		}
	}
	
	/**
	 * This method will be called frequently 
	 * for updating the online users in chat window 
	 * and to populate the chat message for corresponding user
	 * 
	 */
	public void chatHeartBeat(){
		try{
			User user = SecurityUtil.getThreadLocalUser().get();
			System.out.println("In start session====in the heartbeat===>");				
			JSONObject obj = new JSONObject();			
			List<String> userList= cipLoggerService.getAllLoggedFriendsList(user.getId());
			userList.remove(user.getUsername());
			obj.put("username", user.getUsername());
			obj.put("userName", userList);
			List<Chat> chats = chatService.getAllChats(user.getUsername());
			List chatArrayList = new ArrayList();
			for (Chat chat : chats) {
				Map chatobj = new HashMap();
				chatobj.put("s",0);
				chatobj.put("f", chat.getChatFrom());
				chatobj.put("m", chat.getMessage());
			    chatArrayList.add(chatobj);
			    chat.setIsReceived(1);
			    chatService.saveOrUpdateChat(chat);	    
			}
			obj.put("items", chatArrayList);	
			CommonUtil.generateResponseThroughJSON(getResponse(), obj);
		}catch(Exception e){
			e.printStackTrace();
		}
		
	}
	
	public ChatService getChatService() {
		return chatService;
	}
	public void setChatService(ChatService chatService) {
		this.chatService = chatService;
	}

	public CIPLoggerService getCipLoggerService() {
		return cipLoggerService;
	}

	public void setCipLoggerService(CIPLoggerService cipLoggerService) {
		this.cipLoggerService = cipLoggerService;
	}	
}
